package com.example.account.service.impl;

import com.example.account.service.AccountService;
import io.seata.core.context.RootContext;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AccountService.class);

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public int debit(String userId, int money) {
        LOGGER.info("Account Service ... xid: " + RootContext.getXID());
        LOGGER.info("Deducting balance SQL: update account_tbl set money = money - {} where user_id = {}", money, userId);
        int result = jdbcTemplate.update("update account_tbl set money = money - ? where user_id = ?", money, userId);
        LOGGER.info("Account Service End ... ");
        return result;
    }
}
